<!--
  Copyright 2018 The Outline Authors

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<link rel='import' href='../bower_components/paper-dialog/paper-dialog.html'>
<link rel='import' href='../bower_components/paper-dialog-scrollable/paper-dialog-scrollable.html'>

<dom-module id='outline-share-dialog'>
  <template>
    <style include='cloud-install-styles'></style>
    <style>
      :host {
        margin: 0px;
      }
      a {
        color: #009485;
      }
      #dialog {
        display: flex;
        flex-flow: column nowrap;
        width: 100%;
        padding: 24px;
      }
      #dialog-header {
        text-align: left;
        margin: 0 0 18px 0;
        padding: 0;
      }
      #dialog-header h3 {
        font-size: 18px;
        color: rgba(0,0,0,0.87);
        opacity: 0.87;
        line-height: 24px;
        padding: 0;
      }
      #dialog-header p {
        font-size: 14px;
        color: rgba(0,0,0,0.54);
        line-height: 20px;
        max-width: 85%;
        margin: 0;
      }
      #selectableText {
        height: 144px;
        overflow: auto;
        background-color: #ECEFF1;
        border-radius: 2px;
        margin: 0;
        padding: 18px;
        font-size: 12px;
        line-height: 18px;
      }
      #selectableText p {
        color: black;
        padding: 0;
        margin-top: 0;
        margin-bottom: 14px;
      }
      #selectableText a {
        text-decoration: underline;
        color: #009485;
        font-weight: 500;
        border: none;
      }
      #copyText {
        text-align: center;
        color: rgba(0, 0, 0, 0.54);
        margin: 0;
      }
      #button-row {
        margin: 24px 0;
        padding: 0;
        letter-spacing: 0.62px;
      }
      #copyButton {
        color: #f1f2f3;
        background-color: #263238;
      }
      #doneButton {
        color: #009485;
        right: 0;
        position: absolute;
      }
    </style>
    <paper-dialog id="dialog">
      <div id="dialog-header">
        <h3>Share access</h3>
        <p>
          Copy this invitation and send it from a communication tool you trust. <a href="https://securityplanner.org/#/all-recommendations">Need help?</a>
        </p>
      </div>
      <div contenteditable id="selectableText" style="-webkit-text-size-adjust: 100%;">
        <p>You’re invited to connect to my Outline server. Use it to access the open internet, no matter where you are. Follow the instructions on your invitation link below to download the Outline App and get connected.</p>

        <p><a href$="{{s3Url}}">{{s3Url}}</a></p>
        <p>-----</p>
        <p>Having trouble accessing the invitation link?</p>
        <ol>
          <li>
            Copy your access key:
            <a href="{{accessUrl}}">{{accessUrl}}</a>
          </li>
          <li>
            Follow our invitation instructions on GitHub:
            <a href="https://github.com/Jigsaw-Code/outline-client/blob/master/docs/invitation-instructions.md">https://github.com/Jigsaw-Code/outline-client/blob/master/docs/invitation-instructions.md</a>
          </li>
        </ol>
      </div>
      <div id="button-row">
        <paper-button id="copyButton" on-tap="copyClicked">Copy invitation</paper-button>
        <paper-button id="doneButton" dialog-confirm>Done</paper-button>
      </div>
      <div id="copyText" hidden>Copied to clipboard</div>
    </paper-dialog>
  </template>
  <script>
    Polymer({
      is: 'outline-share-dialog',
      created: function () {
        this.clipboard = require('clipboard-polyfill');
      },
      open: function(accessUrl) {
        this.accessUrl = accessUrl;
        // If updating this URL, must also update it in outline-server-view.html. TODO: deduplicate
        this.s3Url = 'https://s3.amazonaws.com/outline-vpn/invite.html#' +
            encodeURIComponent(accessUrl);
        this.$.copyText.setAttribute('hidden', true);
        this.$.dialog.open();
      },
      copyClicked: function() {
        var dt = new this.clipboard.DT();
        dt.setData("text/plain", this.$.selectableText.innerText);
        dt.setData("text/html", this.$.selectableText.innerHTML);
        this.clipboard.write(dt);
        this.$.copyText.removeAttribute('hidden');
      }
    });
  </script>
</dom-module>
